<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>npm cnpm npx nvm 傻傻分不清 | 前端一锅煮</title>
    <meta name="description" content="npm cnpm npx nvm 傻傻分不清">
    <link rel="icon" href="/blog/logo.jpg">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileColor" content="#000000">
    <meta name="keywords" content="前端一锅煮,npm cnpm npx nvm">
    <link rel="preload" href="/blog/assets/css/0.styles.3a36f1f3.css" as="style"><link rel="preload" href="/blog/assets/js/app.2d50d9dc.js" as="script"><link rel="preload" href="/blog/assets/js/26.a4d6759f.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cf06b472.js"><link rel="prefetch" href="/blog/assets/js/11.57a175c6.js"><link rel="prefetch" href="/blog/assets/js/12.b32f2db2.js"><link rel="prefetch" href="/blog/assets/js/13.818a88b3.js"><link rel="prefetch" href="/blog/assets/js/14.1c54dd87.js"><link rel="prefetch" href="/blog/assets/js/15.40ea867f.js"><link rel="prefetch" href="/blog/assets/js/16.379721e3.js"><link rel="prefetch" href="/blog/assets/js/17.0b8bcd15.js"><link rel="prefetch" href="/blog/assets/js/18.41bb3889.js"><link rel="prefetch" href="/blog/assets/js/19.6362af40.js"><link rel="prefetch" href="/blog/assets/js/2.55dfcea5.js"><link rel="prefetch" href="/blog/assets/js/20.4ce40a8a.js"><link rel="prefetch" href="/blog/assets/js/21.ba4dcc65.js"><link rel="prefetch" href="/blog/assets/js/22.d2fad5dd.js"><link rel="prefetch" href="/blog/assets/js/23.2a976f6d.js"><link rel="prefetch" href="/blog/assets/js/24.a6407b77.js"><link rel="prefetch" href="/blog/assets/js/25.28818591.js"><link rel="prefetch" href="/blog/assets/js/27.fe72fdf4.js"><link rel="prefetch" href="/blog/assets/js/28.ca4a85a9.js"><link rel="prefetch" href="/blog/assets/js/29.4c6c0c28.js"><link rel="prefetch" href="/blog/assets/js/3.e235257b.js"><link rel="prefetch" href="/blog/assets/js/30.6aa9a972.js"><link rel="prefetch" href="/blog/assets/js/31.b7bccc14.js"><link rel="prefetch" href="/blog/assets/js/32.3544b6a6.js"><link rel="prefetch" href="/blog/assets/js/33.3bb697fd.js"><link rel="prefetch" href="/blog/assets/js/34.ac7cecf3.js"><link rel="prefetch" href="/blog/assets/js/35.b26fe96f.js"><link rel="prefetch" href="/blog/assets/js/36.91eb88f8.js"><link rel="prefetch" href="/blog/assets/js/37.baa3c248.js"><link rel="prefetch" href="/blog/assets/js/4.04108111.js"><link rel="prefetch" href="/blog/assets/js/5.2e3bc3e6.js"><link rel="prefetch" href="/blog/assets/js/6.88208dc4.js"><link rel="prefetch" href="/blog/assets/js/7.7913f494.js"><link rel="prefetch" href="/blog/assets/js/8.64ff3aa5.js"><link rel="prefetch" href="/blog/assets/js/9.43d70a25.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.3a36f1f3.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">前端一锅煮</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="/blog/page/list/" class="nav-link router-link-active">全部文章</a></div><div class="nav-item"><a href="/blog/page/vuepress/" class="nav-link">VuePress笔记</a></div><div class="nav-item"><a href="/blog/page/about/" class="nav-link">关于我</a></div> <a href="https://gitee.com/cjm0/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="/blog/page/list/" class="nav-link router-link-active">全部文章</a></div><div class="nav-item"><a href="/blog/page/vuepress/" class="nav-link">VuePress笔记</a></div><div class="nav-item"><a href="/blog/page/about/" class="nav-link">关于我</a></div> <a href="https://gitee.com/cjm0/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>npm cnpm npx nvm 傻傻分不清</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/blog/page/list/npm.html#npm" class="sidebar-link">npm</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/page/list/npm.html#cnpm" class="sidebar-link">cnpm</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/page/list/npm.html#npx" class="sidebar-link">npx</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/page/list/npm.html#nvm" class="sidebar-link">nvm</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="npm-cnpm-npx-nvm-傻傻分不清"><a href="#npm-cnpm-npx-nvm-傻傻分不清" class="header-anchor">#</a> npm cnpm npx nvm 傻傻分不清</h1> <p>用过 npm cnpm吗？知道 npx nvm 吗？</p> <p>唔~</p> <p>且看下文分解。</p> <h2 id="npm"><a href="#npm" class="header-anchor">#</a> npm</h2> <p>npm 的全称是 Node Package Manager 是 JavaScript 世界的包管理工具，并且是 Node.js 平台的默认包管理工具，在安装的 nodejs 的时候，npm 会跟着一起安装。通过 npm 可以安装、共享、分发代码,管理项目依赖关系。</p> <p><a href="http://www.cnblogs.com/PeunZhang/p/5553574.html" target="_blank" rel="noopener noreferrer">常用命令<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">npm</span> -v 显示版本，检查npm 是否正确安装
<span class="token function">npm</span> <span class="token builtin class-name">help</span> 可查看某条命令的详细帮助，例如npm <span class="token builtin class-name">help</span> <span class="token function">install</span>
<span class="token function">npm</span> list -g  查看已经安装的模块
<span class="token function">npm</span> show express 查看已经安装的模块的详情 
<span class="token function">npm</span> cache clean --force 清除npm本地缓存

<span class="token function">npm</span> init 初始化
<span class="token function">npm</span> <span class="token function">install</span> xxx 下载包
<span class="token function">npm</span> uninstall xxx 卸载包
<span class="token function">npm</span> update xxx 更新包
<span class="token function">npm</span> outdated -g --depth<span class="token operator">=</span><span class="token number">0</span> 要找出需要更新的软件包
</code></pre></div><p><strong>发布一个 npm 包：</strong></p> <ol><li><p>注册 &amp;&amp; 查询：<a href="https://www.npmjs.com" target="_blank" rel="noopener noreferrer">https://www.npmjs.com<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>登录：<code>npm login</code></p></li> <li><p>查询确认成功：<code>npm whoami</code></p></li> <li><p>上传包：<code>npm publish</code></p></li> <li><p>验证邮箱：<code>npm adduser</code></p></li> <li><p>换源：<code>npm config set registry http://registry.npmjs.org</code></p></li> <li><p>更新：修改 version 后 <code>npm publish</code></p></li></ol> <h2 id="cnpm"><a href="#cnpm" class="header-anchor">#</a> cnpm</h2> <p>cnpm 淘宝镜像，可以看成是 npm 的国内版本，下载 npm 包的速度更快。</p> <p>安装 <code>npm install cnpm -g --registry=https://registry.npm.taobao.org</code>。</p> <h2 id="npx"><a href="#npx" class="header-anchor">#</a> npx</h2> <p><code>npm</code> v5.2.0 引入的一条命令。</p> <p><code>npx</code> 会帮你执行依赖包里的二进制文件，引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验。</p> <p>把原来需要全局安装的包放到项目目录下安装。</p> <div class="language-bash extra-class"><pre class="language-bash"><code>old:

<span class="token function">npm</span> <span class="token function">install</span> -g create-react-app

create-react-app my-app

new:

npx create-react-app my-app
</code></pre></div><p>临时安装 <code>create-react-app</code> 包，命令完成后 <code>create-react-app</code> 会删掉，不会出现在 <code>global</code> 中，下次再执行，还是会重新临时安装。</p> <h2 id="nvm"><a href="#nvm" class="header-anchor">#</a> nvm</h2> <p>node 管理工具</p> <p>在开发中，有时候对 node 的版本有要求，有时候需要切换到指定的 node 版本来重现问题等。遇到这种需求的时候，我们需要能够灵活的切换 node 版本，nvm 就是为解决这个问题而产生的，他可以方便的在同一台设备上进行多个 node 版本之间切换。</p> <p>nvm 不支持 Windows，但是有替代品，也就是 nvm-windows。</p> <p><a href="https://github.com/creationix/nvm/blob/master/README.md" target="_blank" rel="noopener noreferrer">安装 nvm<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">wget</span> -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh <span class="token operator">|</span> <span class="token function">bash</span> 
</code></pre></div><p>安装完成后关闭终端，重新打开终端输入 nvm 验证一下是否安装成功，当出现“Node Version Manager”时，说明已安装成功。</p> <p>如果在新的终端输入 nvm 时提示：command not found: nvm，有可能是以下原因之一：</p> <p>你的系统可能缺少一个 .bash_profile 文件，你可以创建一个此文件（可通过vi或vim命令），打开复制粘贴以下代码（安装nvm成功后终端的最好3行代码）进去，保存，然后再次运行安装命令；</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">export</span> <span class="token assign-left variable">NVM_DIR</span><span class="token operator">=</span><span class="token string">&quot;<span class="token environment constant">$HOME</span>/.nvm&quot;</span>
<span class="token punctuation">[</span> -s <span class="token string">&quot;<span class="token variable">$NVM_DIR</span>/nvm.sh&quot;</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">\</span>. <span class="token string">&quot;<span class="token variable">$NVM_DIR</span>/nvm.sh&quot;</span>  <span class="token comment"># This loads nvm</span>
<span class="token punctuation">[</span> -s <span class="token string">&quot;<span class="token variable">$NVM_DIR</span>/bash_completion&quot;</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">\</span>. <span class="token string">&quot;<span class="token variable">$NVM_DIR</span>/bash_completion&quot;</span>  <span class="token comment"># This loads nvm bash_completion</span>
</code></pre></div><p>注意：如果你安装了 oh my zsh ，需要在 .zshrc 文件去添加以上配置信息，（一般安装成功都会自动写入这个文件最底部）。</p> <p>如果上面没有解决问题，打开你的 .bash_profile 文件，并添加以下代码：
<code>source ~/.bashrc</code>，更改完记得保存更改。</p> <p>常用命令：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>nvm ls-remote               列出全部可以安装的版本号
nvm <span class="token function">install</span> stable          安装当前最新的稳定版
nvm <span class="token function">install</span> v10.14.0        安装指定版本
nvm <span class="token function">ls</span>                      显示所有安装的版本
nvm current                 查看当前版本
nvm use v10.14.0            切换node版本
nvm <span class="token builtin class-name">alias</span> default v10.14.0  设置默认版本
</code></pre></div></div> <div class="page-edit"><div class="edit-link"><a href="https://gitee.com/cjm0/blog/edit/master/docs/page/list/npm.md" target="_blank" rel="noopener noreferrer">在 Gitee 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----></div> <!----> </div> <!----></div></div>
    <script src="/blog/assets/js/app.2d50d9dc.js" defer></script><script src="/blog/assets/js/26.a4d6759f.js" defer></script>
  </body>
</html>
